package ee.maoinvest.frontend.controller.requestprocessors;

import java.sql.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ee.maoinvest.backend.model.data.Enterprise;
import ee.maoinvest.backend.model.data.views.EnterpriseContractWithElectricityMeter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriseforms.DeleteEnterpriseFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriseforms.EditEnterpriseFormConverter;
import ee.maoinvest.frontend.controller.formstringdataconverters.enterpriseforms.InsertEnterpriseFormConverter;

public class EnterpriseFormsRequestProcessor 
{
	private final String EMPTY_STRING = "";
	private final int GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR = -1;
	
	public InsertEnterpriseFormConverter fillInsertEnterpriseFormConverterWithRequestData(
			HttpServletRequest request)
	{
		InsertEnterpriseFormConverter insertEnterpriseFormConverter = 
				new InsertEnterpriseFormConverter();
		
		String informalName = request.getParameter("insertEnterpriseInformalName");
		String businessName = request.getParameter("insertEnterpriseBusinessName");
		String registryCode = request.getParameter("insertEnterpriseRegistryCode");
		String dateInserted = request.getParameter("insertEnterpriseDateInserted");
		HttpSession session = request.getSession();
		Date latestDateAfterWhatDataManipulationIsPossible = (Date) session.getAttribute("latestDateAfterWhatDataManipulationIsPossible");
		
		insertEnterpriseFormConverter.setInformalName(informalName);
		insertEnterpriseFormConverter.setBusinessName(businessName);
		insertEnterpriseFormConverter.setRegistryCode(registryCode);
		insertEnterpriseFormConverter.setDateInserted(dateInserted);
		insertEnterpriseFormConverter.setLatestDateAfterWhatDataManipulationIsPossible(latestDateAfterWhatDataManipulationIsPossible);
		
		return insertEnterpriseFormConverter;
	}
	
	public EditEnterpriseFormConverter fillEditEnterpriseFormConverterWithRequestData(
			HttpServletRequest request)
	{
		EditEnterpriseFormConverter editEnterpriseFormConverter = 
				new EditEnterpriseFormConverter();
		
		String enterpriseID = request.getParameter("enterpriseID");
		String informalName = request.getParameter("editEnterpriseInformalName");
		String businessName = request.getParameter("editEnterpriseBusinessName");
		String registryCode = request.getParameter("editEnterpriseRegistryCode");
		HttpSession session = request.getSession();
		Enterprise enterpriseToEdit = (Enterprise) session.getAttribute("enterpriseToEdit");
		
		editEnterpriseFormConverter.setEnterpriseID(enterpriseID);
		editEnterpriseFormConverter.setInformalName(informalName);
		editEnterpriseFormConverter.setBusinessName(businessName);
		editEnterpriseFormConverter.setRegistryCode(registryCode);
		editEnterpriseFormConverter.setEnterpriseToEdit(enterpriseToEdit);
		
		return editEnterpriseFormConverter;
	}
	
	public DeleteEnterpriseFormConverter fillDeleteEnterpriseFormConverterWithRequestData(
			HttpServletRequest request)
	{
		DeleteEnterpriseFormConverter deleteEnterpriseFormConverter = new DeleteEnterpriseFormConverter();
		
		String enterpriseIDThatIsChosenToDelete = request.getParameter("enterpriseIDThatIsChosenToDelete");
		
		HttpSession session = request.getSession();
		Enterprise enterpriseBeingDeleted = (Enterprise) session.getAttribute("enterpriseBeingDeleted");
		@SuppressWarnings("unchecked")
		List<EnterpriseContractWithElectricityMeter> listOfAllEnterpriseContractsWithElectricityMeters = 
				(List<EnterpriseContractWithElectricityMeter>) session.getAttribute(
						"listOfAllEnterpriseContractsWithElectricityMeters");
		
		deleteEnterpriseFormConverter.setEnterpriseIDThatIsChosenToDelete(enterpriseIDThatIsChosenToDelete);
		deleteEnterpriseFormConverter.setEnterpriseBeingDeleted(enterpriseBeingDeleted);
		deleteEnterpriseFormConverter.setListOfAllEnterpriseContractsWithElectricityMeters(
				listOfAllEnterpriseContractsWithElectricityMeters);
		
		return deleteEnterpriseFormConverter;
	}
	
	public int getEditableEnterpriseID(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("enterpriseIDToEdit");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
	public int getEnterpriseIDToDelete(HttpServletRequest request)
	{
		int stringParsedToInt = GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
		
		String enterpriseID = request.getParameter("enterpriseIDToDelete");
		if (enterpriseID != null && !enterpriseID.equals(EMPTY_STRING))
		{
			try 
			{
				stringParsedToInt = Integer.parseInt(enterpriseID);
			}
			catch (Exception e) 
			{
				e.printStackTrace();
				return GET_EDITABLE_ENTERPRISE_ID_PARSE_EXCEPTION_INDICATOR;
			}
		}
		
		return stringParsedToInt;
	}
	
}
